package com.example.front_back_separation_development.controller;

import com.example.front_back_separation_development.dto.ArticleDTO;
import com.example.front_back_separation_development.entity.Article;
import com.example.front_back_separation_development.service.IArticleService;
import com.example.front_back_separation_development.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/device")
public class DeviceController {
    @Autowired
    private IDeviceService service;

    @GetMapping("/findAllPage")
    public Result findAllPage(Integer page, Integer size){
        return Result.success(service.findAllByPaged(page,size));
    }

    @GetMapping("/findByTitleLike")
    public Result findByTitleLike(DevieDTO devieDTO){
        if (devieDTO.getName() == null) {
            return Result.success(service.findAllByPaged(articleDTO.getPage(),articleDTO.getSize()));
        }
    return Result.success(service.findByTitleLike(articleDTO));
    }

    @GetMapping("/deleteById")
    public Result deleteById(ArticleDTO articleDTO){
        Boolean deleteById = service.deleteById(articleDTO);
        if (deleteById){
            return Result.success();
        }
       return Result.error();
    }

    @GetMapping("/addArticle")
    public Result addArticle(ArticleDTO articleDTO){
        Boolean aBoolean = service.addArticle(articleDTO);
        if (aBoolean) {
            return Result.success(service.findAllByPaged(articleDTO.getPage(),articleDTO.getSize()));
        } else {
            return Result.error();
        }
    }

    @GetMapping("/updateArticle")
    public Result updateArticle(ArticleDTO articleDTO){
        Boolean aBoolean = service.updateArticle(articleDTO);
        if (aBoolean) {
            return Result.success();
        } else {
            return Result.error();
        }
    }
}
